﻿using AutoMapper;
using Entity.BaseInfo.DTO.Warehouse;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Warehouse;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace Service.BaseInfo
{
    public class WarehouseService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public WarehouseService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取仓库列表
         */
        public async Task<RequestResponseModel<PageResult<WarehouseView>>> GetWarehouseList(WarehouseSearchDTO warehouseSearchDTO)
        {
            //筛选
            var query = _context.WarehouseView
                .WhereIF(a => a.Code.Contains(warehouseSearchDTO.Code), !string.IsNullOrEmpty(warehouseSearchDTO.Code))
                .WhereIF(a => a.Name.Contains(warehouseSearchDTO.Name), !string.IsNullOrEmpty(warehouseSearchDTO.Name))
                ;

            //返回列表
            var warehouseList = await query
                .OrderBy(a => a.CreateTime)
                .Page(warehouseSearchDTO.CurrentPage, warehouseSearchDTO.PageSize)
                .ToListAsync();

            //总数据量
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<WarehouseView>(warehouseList, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增仓库
         */
        public async Task<RequestResponseModel<string>> AddWarehouse(WarehouseAddDTO warehouseAddDTO)
        {
            //新增数据
            var warehouse = _mapper.Map<WarehouseModel>(warehouseAddDTO);
            warehouse.CreateTime = DateTime.Now;
            warehouse.Id = Guid.NewGuid().ToString();

            //更新表
            _context.Warehouse.Add(warehouse);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", warehouse.Id);
        }

        /**
         * 更新仓库
         */
        public async Task<RequestResponseModel<string>> PutWarehouse(WarehouseUpdateDTO warehouseUpdateDTO)
        {
            //校验数据
            var query = await Valid.ValidId<WarehouseModel>(_context.Warehouse, warehouseUpdateDTO.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", warehouseUpdateDTO.Id);
            }

            //编辑数据
            var warehouse = _mapper.Map(warehouseUpdateDTO, query);

            //更新表
            _context.Warehouse.Update(warehouse);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "更新成功", warehouse.Id);
        }

        /**
         * 删除仓库
         */
        public async Task<RequestResponseModel<string>> DeleteWarehouse(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<WarehouseModel>(_context.Warehouse, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //查询仓库下仓位
            var warehousePositionList = await _context.WarehousePosition
                .Where(a => a.WarehouseId == Id)
                .ToListAsync()
                ;

            //判断仓位
            if (warehousePositionList.Count != 0)
            {
                return RequestResponse.CreateResponse(400, "该仓库下有仓位，无法删除", Id);
            }

            //更新表
            _context.Warehouse.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }

        /**
         * 下拉选择仓库
         */
        public async Task<RequestResponseModel<List<WarehouseSelectView>>> GetWarehouseSelectList(WarehouseSelectSearchDTO warehouseSelectSearchDTO)
        {
            //筛选
            var query = await _context.WarehouseSelectView
                .WhereIF(a => a.Code.Contains(warehouseSelectSearchDTO.KeyWord) || a.Name.Contains(warehouseSelectSearchDTO.KeyWord), !string.IsNullOrEmpty(warehouseSelectSearchDTO.KeyWord))
                .ToListAsync();

            //返回数据
            return RequestResponse.CreateResponse(200, "查询成功", query);
        }
    }
}
